#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowIcon(QIcon(":/icon/resource/logo.ico"));
    map=new Astar("Map",this,10,10);

    /*
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect;
     //阴影色，透明度
    shadowEffect->setColor(QColor(100,100,100));
    shadowEffect->setBlurRadius(20);//阴影模糊半径
    shadowEffect->setOffset(5);//阴影的偏移值
    map->setGraphicsEffect(shadowEffect);
    */

    mainarea=new QScrollArea(this);
    mainarea->setAlignment(Qt::AlignCenter);
    mainarea->setWidget(map);

    setStatusBar();
    setMenuBar();
    setToolBar();
    setCentralWidget(mainarea);

    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(setWindowSize()));
    timer->start(300);



}

MainWindow::~MainWindow()
{

}
void MainWindow::setMenuBar(){
    QMenu *fileMenu=menuBar()->addMenu("文件");
    QMenu *settingMenu=menuBar()->addMenu("Settings");
    QMenu *aboutMenu=menuBar()->addMenu("Other");
    QAction *setxyAction=new QAction("设置地图大小");
    QAction *fileopenAction=new QAction("Open File");
    QAction *filesaveAction=new QAction("Save As...");
    QAction *aboutAction=new QAction("About");
    set4dir= new QAction("四方向");
    set4dir->setCheckable(true);
    set8dir= new QAction("八方向");
    set8dir->setCheckable(true);
    QActionGroup *directionsAction=new QActionGroup(this);
    directionsAction->addAction(set4dir);
    directionsAction->addAction(set8dir);

    euAction=new QAction("欧几里得距离");
    euAction->setCheckable(true);
    manAction=new QAction("曼哈顿距离");
    manAction->setCheckable(true);
    diaAction=new QAction("对角最短距离");
    diaAction->setCheckable(true);
    QActionGroup *hfuncAction=new QActionGroup(this);
    hfuncAction->addAction(euAction);
    hfuncAction->addAction(manAction);
    hfuncAction->addAction(diaAction);

    fileMenu->addAction(fileopenAction);
    fileMenu->addAction(filesaveAction);
    fileMenu->addSeparator();

    aboutMenu->addAction(aboutAction);
    aboutMenu->addSeparator();

    settingMenu->addAction(setxyAction);
    settingMenu->addSeparator();
    settingMenu->addAction(set4dir);
    settingMenu->addAction(set8dir);
    settingMenu->addSeparator();
    settingMenu->addAction(euAction);
    settingMenu->addAction(manAction);
    settingMenu->addAction(diaAction);

    connect(setxyAction,SIGNAL(triggered(bool)),this,SLOT(openxyDialog()));
    connect(aboutAction,SIGNAL(triggered(bool)),this,SLOT(openAboutDialog()));

    connect(set4dir,SIGNAL(triggered(bool)),this,SLOT(setdir()));
    connect(set8dir,SIGNAL(triggered(bool)),this,SLOT(setdir()));
    connect(euAction,SIGNAL(triggered(bool)),this,SLOT(sethfunc()));
    connect(manAction,SIGNAL(triggered(bool)),this,SLOT(sethfunc()));
    connect(diaAction,SIGNAL(triggered(bool)),this,SLOT(sethfunc()));

    set8dir->trigger();
    diaAction->trigger();

}
void MainWindow::openxyDialog(){
    SetxyDialog *aa=new SetxyDialog(this);
    //qDebug()<<map;
    if(aa->exec()==QDialog::Accepted){
        delete map;
        map=new Astar("Map",this,aa->x,aa->y);
        setpainter();
        mainarea->setWidget(map);
        setStatusBar(aa->x,aa->y,0,0);
        //map->show();
        //qDebug()<<map;
    }
    delete aa;
    return;
}
void MainWindow::clearmap(){
    delete map;
    map=new Astar("Map",this,map->w,map->h);
    //map->show();
    setpainter();
    mainarea->setWidget(map);
    //qDebug()<<"clear";
}
void MainWindow::setToolBar(){
    QToolBar *barset=addToolBar("工具");
    //barset->setMovable(false);
    barset->setStyleSheet("QToolBar{background-color:rgb(230,230,230);spacing:10;}");
    barset->addSeparator();

    QToolButton *clearBtn=new QToolButton(this);
    clearBtn->setIcon(QIcon(":/icon/resource/clear.png"));
    clearBtn->setText("清空");
    clearBtn->setFont(QFont("微软雅黑",12));
    clearBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    clearBtn->setMinimumSize(81,66);

    barset->addWidget(clearBtn);
    connect(clearBtn,SIGNAL(clicked(bool)),this,SLOT(clearmap()));


    barset->addSeparator();

    btngroup=new QButtonGroup(this);

    startbtn=new QToolButton(this);
    startbtn->setText("设置起点");
    startbtn->setFont(QFont("微软雅黑",12));
    startbtn->setCheckable(true);
    startbtn->setIcon(QIcon(":/icon/resource/start.png"));
    startbtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    endbtn=new QToolButton(this);
    endbtn->setText("设置终点");
    endbtn->setFont(QFont("微软雅黑",12));
    endbtn->setCheckable(true);
    endbtn->setIcon(QIcon(":/icon/resource/end.png"));
    endbtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    blocksbtn=new QToolButton(this);
    blocksbtn->setText("设置障碍");
    blocksbtn->setFont(QFont("微软雅黑",12));
    blocksbtn->setCheckable(true);
    blocksbtn->setIcon(QIcon(":/icon/resource/wall.png"));
    blocksbtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    btngroup->addButton(startbtn);
    btngroup->addButton(endbtn);
    btngroup->addButton(blocksbtn);

    barset->addWidget(startbtn);
    barset->addWidget(endbtn);
    barset->addWidget(blocksbtn);
    connect(startbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));
    connect(endbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));
    connect(blocksbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));

    barset->addSeparator();

    startbtn->click();

    QToolButton *startAstar=new QToolButton(this);

    startAstar->setText("Run!");
    startAstar->setFont(QFont("微软雅黑",12));
    startAstar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    startAstar->setMinimumSize(81,66);

    startAstar->setIcon(QIcon(":/icon/resource/logo.ico"));

    barset->addWidget(startAstar);
    connect(startAstar,SIGNAL(clicked(bool)),this,SLOT(startA()));

    QToolButton *clearways=new QToolButton(this);

    clearways->setText("清除路径");
    clearways->setFont(QFont("微软雅黑",12));
    clearways->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    clearways->setIcon(QIcon(":/icon/resource/erase.png"));

    barset->addWidget(clearways);

    connect(clearways,SIGNAL(clicked(bool)),this,SLOT(clearways()));

    barset->addSeparator();

    barset->setIconSize(QSize(35,35));

}
void MainWindow::setpainter(){
    if(startbtn->isChecked()) map->setpainterstatus(2);
    if(endbtn->isChecked()) map->setpainterstatus(3);
    if(blocksbtn->isChecked()) map->setpainterstatus(0);
}
void MainWindow::startA(){
    map->runAstar();
}
void MainWindow::clearways(){
    map->clearways();

}
void MainWindow::setWindowSize(){
    int ww=this->width();
    int hh=this->height();
    qDebug()<<ww<<hh;
    this->setMinimumSize(ww,hh);
    //qDebug()<<startbtn->size();
    timer->stop();
}
void MainWindow::openAboutDialog(){
    AboutDialog *about=new AboutDialog(this);
    about->show();
}
void MainWindow::sethfunc(){
    if(euAction->isChecked()){
        map->sethfunc(3);
        setStatusBar(map->w,map->h,0,3);
    }else if(manAction->isChecked()){
        map->sethfunc(2);
        setStatusBar(map->w,map->h,0,2);
    }else{
        map->sethfunc(1);
        setStatusBar(map->w,map->h,0,1);
    }
}
void MainWindow::setdir(){
    if(set4dir->isChecked()){
        map->setdir(2);
        setStatusBar(map->w,map->h,2,0);
    }else{
        map->setdir(1);
        setStatusBar(map->w,map->h,1,0);
    }
}

void MainWindow::setStatusBar(){
    widthStatus=new QLabel("宽度：10");
    heightStatus=new QLabel("高度：10");
    dirStatus=new QLabel("当前方向：八方向");
    hfuncStatus=new QLabel("当前启发函数：对角最短距离");

    statusBar()->addPermanentWidget(dirStatus);
    statusBar()->addPermanentWidget(hfuncStatus);
    statusBar()->addWidget(widthStatus);
    statusBar()->addWidget(heightStatus);

    statusBar()->setFont(QFont("SimSun",11));
}

void MainWindow::setStatusBar(int x,int y,int dir,int hfunc){
    widthStatus->setText(QString("宽度：")+QString::number(x));
    heightStatus->setText(QString("高度：")+QString::number(y));
    switch(dir){
    case 1:
        dirStatus->setText(QString("当前方向：八方向"));
        break;
    case 2:
        dirStatus->setText(QString("当前方向：四方向"));
        break;
    }
    switch(hfunc){
    case 1:
        hfuncStatus->setText(QString("当前启发函数：对角最短距离"));
        break;
    case 2:
        hfuncStatus->setText(QString("当前启发函数：曼哈顿距离"));
        break;
    case 3:
        hfuncStatus->setText(QString("当前启发函数：欧几里得距离"));
        break;
    }

}

